package com.xxl.job.admin.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.xxl.job.admin.core.model.JobGroup;
import com.xxl.job.admin.core.model.JobInfo;
import com.xxl.job.admin.core.model.JobLog;
import com.xxl.job.admin.dao.IJobGroupDao;
import com.xxl.job.admin.dao.IJobInfoDao;
import com.xxl.job.admin.dao.IJobLogDao;

/**
 * index controller
 * @author xuxueli 2015-12-19 16:13:16
 */
@Controller
@RequestMapping("/joblog")
public class JobLogController {

	@Resource
	private IJobGroupDao xxlJobGroupDao;
	@Resource
	public IJobInfoDao xxlJobInfoDao;
	@Resource
	IJobLogDao xxlJobLogDao;

	@RequestMapping
	public String index(Model model, @RequestParam(required = false, defaultValue = "0") Long jobId) {

		// 执行器列表
		List<JobGroup> jobGroupList =  xxlJobGroupDao.findAll();
		model.addAttribute("JobGroupList", jobGroupList);

		// 任务
		if (jobId > 0) {
			JobInfo jobInfo = xxlJobInfoDao.loadById(jobId);
			model.addAttribute("jobInfo", jobInfo);
		}

		return "joblog/joblog.index.ftl";
	}
	@RequestMapping("/logDetailPage")
	public String logDetailPage(Long id, Model model) {

		// base check
		// ReturnT<String> logStatue = ReturnT.SUCCESS;
		JobLog jobLog = xxlJobLogDao.load(id);
		if (jobLog == null) {
            throw new RuntimeException("抱歉，日志ID非法.");
		}

        model.addAttribute("triggerCode", jobLog.getTriggerCode());
        model.addAttribute("handleCode", jobLog.getHandleCode());
        model.addAttribute("executorAddress", jobLog.getExecutorAddress());
        model.addAttribute("triggerTime", jobLog.getTriggerTime().getTime());
        model.addAttribute("logId", jobLog.getId());
		return "joblog/joblog.detail.ftl";
	}

}
